<template>
    <div>
      <el-input class="search-input" size="mini"
                placeholder="请输入要查找的账号"
                v-model="value"
                @keyup.enter.native="query"
                prefix-icon="el-icon-search"></el-input>
    </div>
</template>

<script>
  import {validUserid} from "@/utils/validate";
  import {searchUserById} from '@/api/user';
  import {mapGetters} from 'vuex'
  export default {
        name: "Search",
      data(){
          return {
            value:''
          }
      },
    computed:{
      ...mapGetters([
        'friendList','id'
      ])
    },
      methods:{
        query(){
          let user= this.friendList.filter(item=>{
            return item.userid === this.value
          })[0]
          if(user){
            this.$store.commit('chat/SET_CURRENT_FRIEND_INFO',user)
            this.$store.commit('chat/SET_FRIEND_ACTIVE','friend-info')
            this.$store.commit('chat/SET_SIDEBAR_ACTIVE','friend-list')
            return
          }else if(this.value ===this.id){
            this.$store.commit('chat/SET_SIDEBAR_ACTIVE','personal-center')
            return;
          }
          // 校验账号合法性
          if(validUserid(this.value)){
            console.log(this.value);
            searchUserById(this.value).then(res=>{
              const {content} = res
              if(content.user){
                console.log(content.user)
                this.$store.commit('chat/SET_CURRENT_FRIEND_INFO',content.user)
                console.log(this.$store.state.chat.currentFriendInfo);
                this.$store.commit('chat/SET_FRIEND_ACTIVE', 'friend-info')
                this.$store.commit('chat/SET_SIDEBAR_ACTIVE','friend-list')
                return
              }
              this.$message.info("该账号不存在！")
            })
          }
        }
      }
    }
</script>

<style scoped>

</style>
